﻿using System.Text.RegularExpressions;
using mesan.core.Repository;

namespace mesan.core.Managers
{
	public class LoginManager : ILoginManager
	{
		private readonly IPasswordRequester _passwordRequester;
		private readonly IValidationRepository _validationRepository;

		public LoginManager(IPasswordRequester passwordRequester, IValidationRepository validationRepository)
		{
			_passwordRequester = passwordRequester;
			_validationRepository = validationRepository;
		}

		public void RequestPasswordReset(string emailAddress)
		{
			if (string.IsNullOrEmpty(emailAddress))
				return;

			string pattern = @"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";
			Match match = Regex.Match(emailAddress.Trim(), pattern, RegexOptions.IgnoreCase);

			if (!match.Success)
				return;

			_passwordRequester.RequestByEmail(emailAddress);
		}

		public bool Validate(string userName, string password)
		{
			if (string.IsNullOrEmpty(userName))
				return false;

			if (string.IsNullOrEmpty(password))
				return false;

			return _validationRepository.Validate(userName, password);
		}
	}
}
